#include <iostream>
#include <fstream>

using namespace std;
class List                                    //定义单链表的结构 
{
private:
    double num;

    void set_num(double var)
    {
        this->num = var;
    }

    double get_num()
    {
        return this->num;
    }
    class List *next;
    void set_next(List* var)
    {
        this->next = var;
    }

    List* get_next()
    {
        return this->next;
    }
    void create();
    void input(int r);
    void Listout();
    void seek();
    void instal();
    void dele();
public:
    void choose();
};
typedef class List Node;                     //将class list重定义为node
typedef Node *Link;                            //声明指针
Link p,s,top;
void List::choose()
{
    List List1;
    int r;
    bool toD = true;
    while(toD)
    {
        cout<<"1, input content create a new file"<<endl;
        cout<<"2, print the file"<<endl;
        cout<<"3, find digital in the file"<<endl;
        cout<<"4, inputlt digital in the file"<<endl;
        cout<<"5, delete digital in the file"<<endl;
        cout<<"press the other key, select exit"<<endl;
        cin>>r;
        switch (r)
        {
            case 1:
                List1.create();                   //创建链表
                break;
            case 2:
                List1.Listout();                //显示表中的数据
                break;
            case 3:
                List1.seek();                 //查找链表中的数据
                break;
            case 4:
                List1.instal();                 //在链表中插入数据
                break;
            case 5:
                List1.dele();                 //在链表中删除数据
                break;
            default:
                toD = false;
                break;
        }
    }
}
void List::create()                           //创建链表操作
{
    cout<<"please input content,press 0 to exit"<<endl;
    double r;
    bool toD = true;
    top = new Node;
    this->set_next(top);
    (*top).next = NULL;
    p = top;
    while(toD)
    {
        cin >> r;
        if (r == 0)
        {toD = false;}
        else
        {
            input(r);
//            s = new Node;
//            this->set_num(r);
//            (*p).next = s;
//            (*s).next = NULL;
//            p = (*p).next;                                //工作指针p后移
        }
    }
    Listout();
}
void List::input(int r)                    //进行输入数据操作 
{
    s = new Node;
    s->set_num(r);
    p->set_next(s);
    (*s).next = NULL;
    p = (*p).next;
}
void List::Listout()                              //进行输出操作
{
    ofstream XS("Account.txt",ios::app);
    double r = 0;
	p = (*top).next;
    while(p)
    {
        cout << p->get_num() <<endl;
        r += p->get_num();
        XS << p->get_num();
        XS << "\n";
        p = (*p).next;
    }
    XS << "sum:" << r;
    XS << "\n";
    XS.close();
}
void List::seek()                         // 进行查找操作
{
    int j = 1;
    int i;
	p = (*top).next;
    cout<<"please enter you want find position "<<endl;
	cin>>i;
    while(p && j<i)
    {
        p = (*p).next;
        j++;
    }
    if(!p)
    {
        cout<<"seek error!";
        cout<<endl;
    }
    else
    {
        cout<<p<<"\t"<<"\t"<<(*p).num;
        cout<<endl;
    }
}
void List::instal()                    //进行插入操作 
{
    int j = 0;
    int i;
	p = (*top).next;
	cout<<"please input you want inputl content"<<endl;
	cin>>num;
	cout<<"please input you want inputl position"<<endl;
	cin>>i;
    while(p && j<i-1)
    {
        p = (*p).next;
        j++;
    }
    if(!p)
    {
        cout<<"inputl error!";
        cout<<endl;
    }
    else
    {
        Link s;
        s = new Node;
        (*s).num = num;
        (*s).next = (*p).next;
        (*p).next = s;
    }
    Listout();
}
void List::dele()                       //进行删除操作 
{
    int j = 0;
    int i;
	p = top;
	cout<<"please input you want delete position"<<endl;
	cin>>i;
    while(p && j<i-1)
    {
        p = (*p).next;
        j++;
    }
    if(!p)
    {
        cout<<"delete error!";
        cout<<endl;
    }
    else
    {
        s = new Node;
        s = (*p).next;
        (*p).next = (*s).next;
    }
    Listout();
}

int main()
{
    ofstream XS("Account.txt");
    List List1;                                 //实例化一个类
    List1.choose();
    ifstream OpenFile("Account.txt");
    char line[100];
    while(!OpenFile.eof())
    {
        OpenFile.getline(line,100);
        cout << line << endl;
    }
    OpenFile.close();
    return 0;
}
//    string name;
//    cout << "pleases input you want create filename"
//    cin >> name;
//    string body = "hanli huangfenggu lianqiqi";
//    ofstream XS("Account.txt");
//    XS << "Di Yi Zhang Shanchunxiaozi" << endl;
//    XS << body;
//    XS.close();
